﻿#include"Data.h"
//日期合法性检查
bool Date::CheakDate() const
{
	if ((_month > 0 && _month < 13) && (_day > 0 && _day <= GetMonthDay(_year, _month)))
		return true;
	else
		return false;
}
//<<运算符重载
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "/" << d._month << "/" << d._day << endl;
	return out;
}
//>>运算符重载
istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年/月/日";
		in >> d._year >> d._month >> d._day;
		if (!d.CheakDate())
		{
			cout << "日期非法！请重新输入";
			d.Print();
		}
		else
			break;
	}
	return in;
}
// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheakDate())
	{
		cout << "日期非法：";
		Print();
	}
}
// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}
// 析构函数
Date::~Date()
{
	;//由于Date类没有指向资源，故这里可以不用执行什么东西
}
// 日期+=天数
Date& Date::operator+=(int day)
{
	if (day < 0)//day<0,+=时相当于-=(-day)
	{
		return *this -= (-day);
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))//---有隐式的this指针
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_month = 1;
			++_year;
		}
	}
	return *this;
}
// 日期+天数
Date Date::operator+(int day) const
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)//day<0,-=时相当于+=(-day)
	{
		return *this += (-day);
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
// 日期-天数
Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}
// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
// 后置++
Date Date::operator++(int)
{
	Date tmp = *this;
	tmp += 1;
	return tmp;
}
// 前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
// 后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	tmp -= 1;
	return tmp;
}
// <运算符重载
bool Date::operator < (const Date& d) const
{
	//return *this - d < 0;//不能用日期-日期，下面日期-日期中调用了<,会造成反复来回调用
	if (_year < d._year)
		return true;
	else if (_year == d._year)
	{
		if (_month < d._month)
			return true;
		else if (_month == d._month)
		{
			return _day < d._day;
		}
	}
	return false;
}
// <=运算符重载
bool Date::operator<=(const Date& d) const
{
	return *this == d || *this < d;
}
// >运算符重载
bool Date::operator>(const Date& d) const//>的相反是<=,故 d1>d2 等价于 !(d1<=d2)
{
	return !(*this <= d);
}
// >=运算符重载
bool Date::operator>=(const Date& d) const//>=的相反是<,故 d1>=d2 等价于 !(d1<d2)
{
	return !(*this < d);
}
// ==运算符重载
bool Date::operator==(const Date& d) const
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
// !=运算符重载
bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d) const
{
	int flag = 1;
	int ret = 0;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	while (min < max)
	{
		++min;
		++ret;
	}
	return ret * flag;
}
void meun()
{
	Date d1; Date d2;
	cout << "请输入您要进行的操作" << endl;
	cout << "主要操作为：1.日期+=天数       2.日期-=天数" << endl;
	cout<<  "           3.日期-日期                   " <<endl;         
	int input = 0;
	cin >> input;
	int day;
	switch (input)
	{
	case 1:
		cout << "请输入你要加的日期和天数" << endl;
		cin >> d1;
		cin >> day;
		cout << (d1 += day);
		break;
	case 2:
		cout << "请输入你要减的日期和天数" << endl;
		cin >> d1;
		cin >> day;
		cout << (d1 -= day);
		break;
	case 3:
		cout << "请输入你要减的两个日期,必须是大日期在前，小日期在后的输入格式" << endl;
		cin >> d1 >> d2;
		//cin >> day;
		cout << (d1 -d2 );
		break;
	default:
		cout << "你的选择有问题" << endl;
		break;
	}
}
int main()
{
	meun();
	return 0;
}